package com.walker.pubsub;

import com.rabbitmq.client.*;
import com.walker.BaseConfig;

/**
 * @Description:
 * @ProjectName: walker-step
 * @Package: com.walker.pubsub
 * @ClassName: RecvConsumer01
 * @Author: yangyl
 * @CreateDate: 2020/6/21 13:14
 */
public class RecvConsumer02 {

    public static final String EXCHANGE_01 = "waring.exchange.01";
    public static final String QUEUE_02 = "warning.queue.02";

    public static ConnectionFactory connectionFactory() {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(BaseConfig.HOST);
        connectionFactory.setVirtualHost(BaseConfig.VHOST);
        connectionFactory.setPort(5672);
        connectionFactory.setUsername(BaseConfig.USERNAME);
        connectionFactory.setPassword(BaseConfig.PASSWORD);
        return connectionFactory;
    }

    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = connectionFactory();
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        //申明交换机、队列、绑定关系
        channel.exchangeDeclare(EXCHANGE_01, BuiltinExchangeType.FANOUT);
        channel.queueDeclare(QUEUE_02,true,false,false,null);
        channel.queueBind(QUEUE_02, EXCHANGE_01, "");

        System.out.println("RecvConsumer02 start recv msg...");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(QUEUE_02, true, deliverCallback, consumerTag -> {
        });

    }
}
